package com.ellricky.excute_president.ui.activity.home.task

import android.os.Bundle
import android.view.View
import android.widget.AdapterView
import com.andr.common.ui.adapter.BaseRecyclerAdapter
import com.andr.common.ui.mvp.BasePresenter
import com.andr.common.util.ResUtil
import com.ellricky.baselib.R
import com.ellricky.baselib.http.callback.EListCallback
import com.ellricky.baselib.ui.base.BaseRefreshFragment
import com.ellricky.baselib.util.EDialogUtil
import com.ellricky.excute_president.*


class TaskFragment : BaseRefreshFragment<Void, BasePresenter<Void>, PdTaskEntity>() {

    private var pageType = -1

    companion object {
        fun newInstance(pageType: Int): TaskFragment {
            val fragment = TaskFragment()
            val bundle = Bundle()
            bundle.putInt(KEY_PAGE_TYPE, pageType)
            fragment.arguments = bundle
            return fragment
        }
    }

    override fun initUIView(view: View?) {
        pageType = arguments?.getInt(KEY_PAGE_TYPE) ?: TASK_PAGE_OTHER
        super.initUIView(view)
        hideToolBar()
    }


    override fun getResponseCallback(): EListCallback<PdTaskEntity> {
        return object : EListCallback<PdTaskEntity>(mContext) {
            override fun onSuccess(data: MutableList<PdTaskEntity>?, msg: String?) {
                dataSource = data
                rvView.onLoadFinish(dataSource, refresh)
            }

            override fun onErrorCode(code: String?, msg: String?) {
                super.onErrorCode(code, msg)
                rvView.onRefreshFailed(refresh)
            }

            override fun onHttpError(code: String?, msg: String?) {
                super.onHttpError(code, msg)
                rvView.onRefreshFailed(refresh)
            }
        }
    }

    override fun getRvAdapter(): BaseRecyclerAdapter<PdTaskEntity> {
        return PdTaskAdapter(mContext)
    }

    override fun getUrlPath(): String {
        return when (pageType) {
            TASK_PAGE_KX -> API_TASK_KX
            TASK_PAGE_ZX -> API_TASK_ZX
            TASK_PAGE_DT -> API_TASK_DT
            TASK_PAGE_OTHER -> API_TASK_OTHER
            else -> ""
        }
    }

    override fun onItemClick(adapterView: AdapterView<*>?, view: View?, i: Int, l: Long) {
        super.onItemClick(adapterView, view, i, l)

        jumpTaskDetail(this,
                adapter.get(i).id,
                pageType)
    }


    fun jumpTaskAddPage() {

        EDialogUtil.showTaskAddDialog(mContext) { _, item ->
            val array = ResUtil.getStrArr(context, R.array.base_task_type)
           val pageType = when(item) {
               0 -> TASK_PAGE_KX
               1 -> TASK_PAGE_ZX
               2 -> TASK_PAGE_DT
               else -> TASK_PAGE_OTHER
           }

            jumpTaskAdd(this@TaskFragment, pageType, array[item])
        }

    }

}
